package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public List<CategoryEntity> listWithTree() {
        //查询出所有分类
        List<CategoryEntity> categoryList = baseMapper.selectList(null);
        List<CategoryEntity> levelList = categoryList.stream().filter(categoryEntity -> {
            //筛选出一级菜单
            return categoryEntity.getParentCid() == 0;
        }).map(categoryEntity -> {
            //往根菜单设置子菜单
            categoryEntity.setChildren(this.getChildren(categoryEntity, categoryList));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            //菜单排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return levelList;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //toDo
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = this.findParentPath(catelogId, paths);
        // 反转
        Collections.reverse(parentPath);
        //转成Long[] 数组返回
        return paths.toArray(new Long[paths.size()]);
    }

    @Override
    @Transactional
    public void updateDetail(CategoryEntity category) {
        //先更新了下三级分类表
        this.updateById(category);
        if(!StringUtils.isEmpty(category.getName())){
            categoryBrandRelationService.updateCatelog(category.getCatId(),category.getName());
        }
    }


    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        //这个这条三级分类数据
        CategoryEntity byId = this.getById(catelogId);
        //判断父id等不等于0
        if(byId.getParentCid() != 0){
            //递归去调用
            this.findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(category -> {
            // 遍历所有的category对象的父类id = 等于root的分类id 说明是他的子类
            return root.getCatId().equals(category.getParentCid());
        }).map(menu -> {
            //递归继续找子菜单
            menu.setChildren(getChildren(menu, all));
            return menu;
        }).sorted((menu1, menu2) -> {
            //菜单排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}